package com.health.patient.server.service.impl;

import com.health.common.core.domain.R;
import com.health.common.core.web.domain.AjaxResult;
import com.health.common.security.utils.SecurityUtils;
import com.health.health.common.dto.PmsPatientGroupDto;
import com.health.health.common.dto.PmsPatientRecordDto;
import com.health.patient.server.mapper.PatientMapper;
import com.health.patient.server.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    private PatientMapper patientMapper;

    @Override
    public AjaxResult getPatientGroupList() {

        //获取医生ID
        //Long doctorId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        Long doctorId = 1L;

        List<PmsPatientGroupDto> patientGroupList =patientMapper.getPatientGroupList(doctorId);

        if (patientGroupList!=null){
            return AjaxResult.success(patientGroupList);
        }
        return AjaxResult.error(500,"获取患者分组失败");
    }

    @Override
    public AjaxResult addPatientGroup(String groupName) {

        //获取医生ID
        //Long doctorId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        Long doctorId = 1L;

        //校验数据
        if (groupName==null||groupName.equals("")){
            return AjaxResult.error(500,"分组名称不能为空");
        }
        //判断是否已经添加过此名称
        if (patientMapper.isAddedByGroupName(groupName,doctorId)==1){
            return AjaxResult.error(500,"分组名称已经存在");
        }

        //添加
        if (patientMapper.addPatientGroup(groupName,doctorId)==true){
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.error(500,"添加失败");
        }
    }

    @Override
    public AjaxResult deletePatientGroup(Long groupId) {

        //获取医生ID
        //Long doctorId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        Long doctorId = 1L;

        //删除分组
        Integer i = patientMapper.deletePatientGroup(groupId, doctorId);
        //删除分组和患者中间表
        Integer j = patientMapper.deleteGroupAndPatient(String.valueOf(groupId), doctorId);

        if(i+j>=1){
            return AjaxResult.success("删除成功");
        }

        return AjaxResult.error("删除分组不存在!");
    }

    @Override
    public AjaxResult getPatientListByGroupId(Long groupId) {

        //获取医生ID
        //Long doctorId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        Long doctorId = 1L;

        //查询出分组下的所有患者信息
        List<PmsPatientRecordDto> patientList =patientMapper.getPatientListByGroupId(groupId,doctorId);
        if (null==patientList){
            return AjaxResult.error(500,"分组内患者为空!");
        }

        //获取包含患者的分组信息列表
        patientList.stream().forEach(patientRecordDto -> {
            //获取患者ID
            Long patientRecordId = patientRecordDto.getPatientRecordId();
            List<PmsPatientGroupDto> patientGroupList = patientMapper.getPatientGroupListByPatientId(patientRecordId,doctorId);
            patientRecordDto.setGroupList(patientGroupList);
        });

        return AjaxResult.success(patientList);
    }

    @Transactional
    @Override
    public AjaxResult updatePatientGroup(Long patientRecordId, String groupIds) {

        //获取医生ID
        //Long doctorId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        Long doctorId = 1L;

        //获取选择分组ID
        String[] groupIdArray = groupIds.split(",");

        //删除患者分组信息
        patientMapper.deletePatientGroupByPatientId(patientRecordId,doctorId);

        //添加患者分组信息
        String Str = "";
        for (String groupId : groupIdArray) {
            String groupName = patientMapper.getGroupName(doctorId, groupId);
            Str+=groupName+",";
        }
        String[] groupNameArray = Str.split(",");

        int i = patientMapper.addPatientAndGroup(doctorId, patientRecordId, groupIdArray,groupNameArray);
        if (i>0){
            return AjaxResult.success("更新成功");
        }else {
            return AjaxResult.error(500,"更新失败");
        }

    }

    /**
     * 添加患者记录
     * @param patientRecordDto
     * @return ybz
     */
    @Override
    public R addPatientRecord(PmsPatientRecordDto patientRecordDto) {
        patientMapper.addPatientRecord(patientRecordDto);
        return R.ok();
    }

}
